package com.pureut.storage.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pureut.storage.dto.ImparityDto;
import com.pureut.storage.mapper.PurchaseReceiptMapper;
import com.pureut.storage.mapper.SalesReturnMapper;
import com.pureut.storage.mapper.WipCompletionMapper;
import com.pureut.storage.service.PublicStorageDocumentService;
import com.pureut.storage.utils.DocumentTypeEnum;
import com.pureut.storage.vo.ImparityVo;
import com.sale.common.core.constant.CacheNames;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.common.redis.utils.CacheUtils;
import com.sale.system.api.domain.SysAuxiliary;
import com.sale.system.api.domain.SysDictData;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 入库多个列表查询实现层
 */
@Service
@RequiredArgsConstructor
public class PublicStorageDocumentServiceImpl implements PublicStorageDocumentService {

    /**
     * 采购入库mapper
     */
    private final PurchaseReceiptMapper purchaseReceiptMapper;

    /**
     * 销售退货mapper
     */
    private final SalesReturnMapper salesReturnMapper;

    /**
     * 完工入库mapper
     */
    private final WipCompletionMapper wipCompletionMapper;

    /**
     * 获取多个列表
     */
    @Override
    public TableDataInfo<ImparityDto> getImparityList(ImparityVo imparityVo, PageQuery pageQuery) {
        //单据类型
        List<SysDictData> statusArray = CacheUtils.get(CacheNames.SYS_DICT, "finance_collection_document_type");
        Map<String, String> statusMap = statusArray.stream().collect(Collectors.toMap(SysDictData::getDictValue, SysDictData::getDictLabel));
        //1:采购入库
        if (imparityVo.getReceiptType() == 1) {
            Page<ImparityDto> imparityDtoPage = purchaseReceiptMapper.selectImparityList(pageQuery.build(), imparityVo);
            List<ImparityDto> records = imparityDtoPage.getRecords();
            for (ImparityDto entity : records) {
                //单据类型
                entity.setReceiptType(statusMap.get(String.valueOf(imparityVo.getReceiptType())));
                //辅助属性
                List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + entity.getTenantId(), String.valueOf(entity.getMaterialId()));
                entity.setAuxiliaryAttribute(statsArray);

                if (entity.getInvoicedQuantity() != null && !"0".equals(entity.getInvoicedQuantity())) {
                    //未开票数量
                    if(entity.getActualQuantity() == null){
                        entity.setNoInvoiceQuantity("0");
                    }else {
                        BigDecimal subtract = entity.getActualQuantity().subtract(new BigDecimal(entity.getInvoicedQuantity()));
                        entity.setNoInvoiceQuantity(subtract.toString());
                    }
                    //已开票金额
                    BigDecimal multiply = new BigDecimal(entity.getInvoicedQuantity()).multiply(entity.getUnitPrice());
                    entity.setInvoiceQuantityAmount(multiply.toString());
                    //未开票金额
                    BigDecimal multiply1 = new BigDecimal(String.valueOf(entity.getPlannedWeight())).multiply(entity.getUnitPrice());
                    BigDecimal subtract1 = multiply1.subtract(multiply);
                    entity.setNoInvoiceQuantityAmount(subtract1.toString());
                } else {
                    //未开票数量
                    entity.setNoInvoiceQuantity(String.valueOf(entity.getActualQuantity()));
                    //已开票金额
                    entity.setInvoiceQuantityAmount("0");
                    //未开票金额
                    BigDecimal multiply1 = new BigDecimal(String.valueOf(entity.getPlannedWeight())).multiply(entity.getUnitPrice());
                    entity.setNoInvoiceQuantityAmount(multiply1.toString());
                }
            }
            return TableDataInfo.build(imparityDtoPage);
        }
        //2：完工入库
        if (imparityVo.getReceiptType() == 2) {
            Page<ImparityDto> imparityDtoPage = wipCompletionMapper.selectImparityList(pageQuery.build(), imparityVo);
            List<ImparityDto> records = imparityDtoPage.getRecords();
            for (ImparityDto entity : records) {
                //单据类型
                entity.setReceiptType(statusMap.get(String.valueOf(imparityVo.getReceiptType())));
                //辅助属性
                List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + entity.getTenantId(), String.valueOf(entity.getMaterialId()));
                entity.setAuxiliaryAttribute(statsArray);
            }
            return TableDataInfo.build(imparityDtoPage);
        }
        //3：其他入库

        //4：销售退货
        if (imparityVo.getReceiptType() == 4) {
            Page<ImparityDto> imparityDtoPage = salesReturnMapper.selectImparityList(pageQuery.build(), imparityVo);
            List<ImparityDto> records = imparityDtoPage.getRecords();
            for (ImparityDto entity : records) {
                //单据类型
                entity.setReceiptType(statusMap.get(String.valueOf(imparityVo.getReceiptType())));
                //辅助属性
                List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + entity.getTenantId(), String.valueOf(entity.getMaterialId()));
                entity.setAuxiliaryAttribute(statsArray);
                if (entity.getInvoicedQuantity() != null && !"0".equals(entity.getInvoicedQuantity())) {
                    //未开票数量
                    BigDecimal subtract = entity.getActualQuantity().subtract(new BigDecimal(entity.getInvoicedQuantity()));
                    entity.setNoInvoiceQuantity(subtract.toString());
                    //已开票金额
                    BigDecimal multiply = new BigDecimal(entity.getInvoicedQuantity()).multiply(entity.getUnitPrice());
                    entity.setInvoiceQuantityAmount(multiply.toString());
                    //未开票金额
                    BigDecimal multiply1 = new BigDecimal(String.valueOf(entity.getPlannedWeight())).multiply(entity.getUnitPrice());
                    BigDecimal subtract1 = multiply1.subtract(multiply);
                    entity.setNoInvoiceQuantityAmount(subtract1.toString());
                } else {
                    //未开票数量
                    entity.setNoInvoiceQuantity(String.valueOf(entity.getActualQuantity()));
                    //已开票金额
                    entity.setInvoiceQuantityAmount("0");
                    //未开票金额
                    BigDecimal multiply1 = new BigDecimal(String.valueOf(entity.getPlannedWeight())).multiply(entity.getUnitPrice());
                    entity.setNoInvoiceQuantityAmount(multiply1.toString());
                }
            }
            return TableDataInfo.build(imparityDtoPage);
        }

        return null;
    }

    /**
     * 查询采购入库数据
     *
     * @param imparityVo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<ImparityDto> getReceiptList(ImparityVo imparityVo, PageQuery pageQuery) {

        Page<ImparityDto> imparityDtoPage = purchaseReceiptMapper.selectImparityList(pageQuery.build(), imparityVo);
        List<ImparityDto> records = imparityDtoPage.getRecords();
        for (ImparityDto entity : records) {
            //未开票数量
            if (entity.getInvoicedQuantity() == null) {
                entity.setNoInvoiceQuantity(String.valueOf(entity.getPlannedWeight()));
                //未开票金额
                BigDecimal multiply1 = new BigDecimal(String.valueOf(entity.getPlannedWeight())).multiply(new BigDecimal(String.valueOf(entity.getUnitPrice())));
                entity.setNoInvoiceQuantityAmount(multiply1.toString());
                //已开票金额
                entity.setInvoiceQuantityAmount("0");
            } else {
                BigDecimal subtract = new BigDecimal(String.valueOf(entity.getPlannedWeight())).subtract(new BigDecimal(entity.getInvoicedQuantity()));
                entity.setNoInvoiceQuantity(subtract.toString());
                //未开票金额
                BigDecimal multiply1 = subtract.multiply(new BigDecimal(String.valueOf(entity.getUnitPrice())));
                entity.setNoInvoiceQuantityAmount(multiply1.toString());
                //已开票金额
                BigDecimal multiply = new BigDecimal(String.valueOf(entity.getUnitPrice())).multiply(new BigDecimal(entity.getInvoicedQuantity()));
                entity.setInvoiceQuantityAmount(multiply.toString());
            }
            //辅助属性
            List<SysAuxiliary> statsArray = CacheUtils.get(CacheNames.SYS_GINSENG + "_" + entity.getTenantId(), String.valueOf(entity.getMaterialId()));
            entity.setAuxiliaryAttribute(statsArray);
            //单据类型
            entity.setReceiptType(DocumentTypeEnum.getValue(Integer.parseInt(entity.getReceiptType())));
        }
        return TableDataInfo.build(imparityDtoPage);
    }
}
